package cn.itcast.option;

import java.util.Optional;
import java.util.function.Consumer;

public class Test02 {
    public static void main(String[] args) {
        String uName ="不为空测试";
        Optional<String> optional = Optional.ofNullable(uName);
        //原先的非空判断
        Boolean present =  optional.isPresent();
        if (present){
            System.out.println(optional.get());
        }

        System.out.println("-----------------------------");
        //userName 不为空的情况下 直接调用accept 打印值 uName ==null 的情况下 就不会执行accept
        optional.ifPresent(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("-----------------------------");

        //改为lambda表达式
        optional.ifPresent(s -> System.out.println(s));
    }
}
